package com.csq.cms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.csq.cms.service.RedisService;
import com.csq.common.util.JsonUtils;
import com.csq.manager.pojo.Account;
import com.csq.manager.pojo.Enterprise;

@Service
public class RedisServiceImpl implements RedisService {

	@Value("${SESSON_KEY}")
	private String SESSON_KEY;

	@Value("${SESSON_EXPIE}")
	private int SESSON_EXPIE;

	@Autowired
	private PoolJedisClient poolJedisClient;

	@Override
	public Account getRedisAccount(String username) {

		if (username == null) {
			return null;
		}

		String token = DigestUtils.md5DigestAsHex(username.getBytes());

		// 写入redis 缓存
		String string = poolJedisClient.get(SESSON_KEY + ":" + token);

		if (string != null) {
			Account account = (Account) JsonUtils.jsonToObject(string,
					Account.class);

			// 重新更新时间
			poolJedisClient.expire(SESSON_KEY + ":" + token, SESSON_EXPIE);

			return account;
		}
		return null;
	}

	@Override
	public Enterprise getRedisEnterprise(String username) {
		// TODO Auto-generated method stub
		if (username == null) {
			return null;
		}

		String token = DigestUtils.md5DigestAsHex(username.getBytes());

		// 写入redis 缓存
		String string = poolJedisClient.get(SESSON_KEY + ":" + token);

		if (string != null) {
			Enterprise enterprise = (Enterprise) JsonUtils.jsonToObject(string,
					Enterprise.class);

			// 重新更新时间
			poolJedisClient.expire(SESSON_KEY + ":" + token, SESSON_EXPIE);

			return enterprise;
		}
		return null;
	}

}
